package com.rw.coco.handler.message.reply.handler.validate;

import com.rw.coco.api.log.CocoLogger;
import com.rw.coco.api.modle.Account;
import com.rw.coco.api.modle.Bot;
import com.rw.coco.api.modle.Group;
import com.rw.coco.handler.message.event.CocoGroupMessageEvent;
import com.rw.coco.handler.message.event.CocoMessageEvent;
import com.rw.coco.handler.message.event.CocoPrivateMessageEvent;
import com.rw.coco.handler.message.reply.ReplyHandler;
import com.rw.coco.util.CocoCommandUtil;
import com.rw.coco.util.CocoReplyUtil;
import com.rw.tool.util.string.StringFormatter;
import com.rw.tool.util.string.StringUtil;

/**
 * 退群、删除好友指令
 */
public class DismissReplyHandler implements ReplyHandler {
    @Override
    public boolean trigger(CocoMessageEvent cocoMessageEvent) {
        if (!cocoMessageEvent.hasPrefix()) {
            return false;
        }

        // 如果是群聊，但权限不足
        if (cocoMessageEvent instanceof CocoGroupMessageEvent) {
            CocoGroupMessageEvent groupMessageEvent = (CocoGroupMessageEvent) cocoMessageEvent;
            if (!groupMessageEvent.isAdmin() && !groupMessageEvent.getAccountModel().isSuperAdmin()) {
                return false;
            }
        }

        // 退群、删除好友指令
        String command = CocoCommandUtil.COMMAND_PROP.get(CocoCommandUtil.DISMISS);

        // 确认是否匹配
        return StringUtil.equalsIgnoreCase(cocoMessageEvent.getMessage(), command);
    }

    @Override
    public void handler(CocoMessageEvent cocoMessageEvent) {
        // 三大基础元素
        Bot bot = cocoMessageEvent.getBot();
        Account sender = cocoMessageEvent.getSender();
        CocoLogger cocoLogger = cocoMessageEvent.getCocoLogger();

        if (cocoMessageEvent instanceof CocoGroupMessageEvent) {
            // 如果是群消息，则退群
            CocoGroupMessageEvent cocoGroupMessageEvent = (CocoGroupMessageEvent) cocoMessageEvent;
            cocoGroupMessageEvent.sendBack(CocoReplyUtil.REPLY_PROP.get(CocoReplyUtil.DISMISS_GROUP));
            cocoGroupMessageEvent.quit();

            // 输出日志
            final String exitGroup = "得到 {}({}) 退出群聊指令，{}({}) 主动退出群聊群 {}({})";
            Group group = cocoGroupMessageEvent.getGroup();
            String msg = StringFormatter.format(exitGroup, sender.getNick(), sender.getId(), bot.getName(), bot.getId(),
                    group.getName(), group.getId());
            cocoLogger.info(msg);
        } else if (cocoMessageEvent instanceof CocoPrivateMessageEvent) {
            // 如果是私聊消息，则删好友
            CocoPrivateMessageEvent cocoPrivateMessageEvent = (CocoPrivateMessageEvent) cocoMessageEvent;
            cocoPrivateMessageEvent.sendBack(CocoReplyUtil.REPLY_PROP.get(CocoReplyUtil.DISMISS_PRIVATE));
            cocoPrivateMessageEvent.delete();

            // 输出日志
            final String deleteFriend = "得到 {}({}) 退出删除好友指令，{}({}) 主动删除好友";
            String msg = StringFormatter.format(deleteFriend, sender.getNick(), sender.getId(), bot.getName(),
                    bot.getId());
            cocoLogger.info(msg);
        } else {
            cocoMessageEvent.getCocoLogger().error("Unknown message event!");
        }
    }
}
